﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProxyWrapper.CommonProxyService;
public partial class ServiceAdvanced : AppServices
{
    string[] selectedValues = null;
    public ServiceProperties srvProperties = null;
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        try
        {
            ProxyWrapper.ServicesServiceWrapper wrapper = new ProxyWrapper.ServicesServiceWrapper();
            srvProperties = wrapper.GetServiceProperties(UserDetails.ClientId, ServiceId);
            selectedValues = srvProperties.AllowedBarCodes != null ? srvProperties.AllowedBarCodes.Split(',') : null;
            if(Request.Form["serviceid"]!=null)
            {
                UpdateQuestionsAssociation();
            }
        }
        catch (Exception)
        {

        }
    }

    private void UpdateQuestionsAssociation()
    {
        try
        {
            ProxyWrapper.ServicesProxy.Questions question = null;// new ProxyWrapper.ServicesProxy.Questions();
            ProxyWrapper.QuestionsServiceWrapper _questions = null;// new ProxyWrapper.QuestionsServiceWrapper();
            string preSubmit = Request.Form["order_pre_submit[]"] != null ? Request.Form["order_pre_submit[]"].ToString():"";
            string postSubmit = Request.Form["order_post_submit[]"] != null ? Request.Form["order_post_submit[]"].ToString() : "";
            string validSubmit = Request.Form["order_valid_scan[]"] != null ? Request.Form["order_valid_scan[]"].ToString() : "";
            string invalidSubmit = Request.Form["order_invalid_scan[]"] != null ? Request.Form["order_invalid_scan[]"].ToString() : "";
            int serviceId = Convert.ToInt32(Request.Form["serviceid"].ToString());
           if(!string.IsNullOrWhiteSpace(preSubmit))
           {
               string[] arrPreSubmit = preSubmit.Split(',');
               foreach (string item in arrPreSubmit)
               {
                   question = new ProxyWrapper.ServicesProxy.Questions();
                   _questions = new ProxyWrapper.QuestionsServiceWrapper();
                   question.QuestionId = Convert.ToInt32(item);
                   question.IsAfterSubmittingScan = false;
                   question.IsBeforSubmittingScan = true;
                   question.IsSubmittingInValidScan = false;
                   question.IsSubmittingValidScan = false;
                   _questions.AssociateQuestions(question, serviceId);
               }
           }
           if (!string.IsNullOrWhiteSpace(postSubmit))
           {
               string[] arrPostSubmit = postSubmit.Split(',');
               foreach (string item in arrPostSubmit)
               {
                   question = new ProxyWrapper.ServicesProxy.Questions();
                   _questions = new ProxyWrapper.QuestionsServiceWrapper();
                   question.QuestionId = Convert.ToInt32(item);
                   question.IsAfterSubmittingScan = true;
                   question.IsBeforSubmittingScan = false;
                   question.IsSubmittingInValidScan = false;
                   question.IsSubmittingValidScan = false;
                   _questions.AssociateQuestions(question, serviceId);
               }
           }
           if (!string.IsNullOrWhiteSpace(validSubmit))
           {
               string[] arrValidSubmit = validSubmit.Split(',');
               foreach (string item in arrValidSubmit)
               {
                   question = new ProxyWrapper.ServicesProxy.Questions();
                   _questions = new ProxyWrapper.QuestionsServiceWrapper();
                   question.QuestionId = Convert.ToInt32(item);
                   question.IsAfterSubmittingScan = false;
                   question.IsBeforSubmittingScan = false;
                   question.IsSubmittingInValidScan = false;
                   question.IsSubmittingValidScan = true;
                   _questions.AssociateQuestions(question, serviceId);
               }
           }
           if (!string.IsNullOrWhiteSpace(invalidSubmit))
           {
               string[] arrInvalidSubmit = invalidSubmit.Split(',');
               foreach (string item in arrInvalidSubmit)
               {
                   question = new ProxyWrapper.ServicesProxy.Questions();
                   _questions = new ProxyWrapper.QuestionsServiceWrapper();
                   question.QuestionId = Convert.ToInt32(item);
                   question.IsAfterSubmittingScan = false;
                   question.IsBeforSubmittingScan = false;
                   question.IsSubmittingInValidScan = true;
                   question.IsSubmittingValidScan = false;
                   _questions.AssociateQuestions(question, serviceId);
               }
           }
          
           
         
           
        }
        catch (Exception)
        {
        }
    }

    internal string LoadBarCodeFormates()
    {
        string sel = "";
        Dictionary<string, string> timeZone = StaticData.GetBarcodeFormat();
        StringBuilder sb = new StringBuilder();
        foreach (KeyValuePair<string, string> item in timeZone)
        {
            if (selectedValues != null && selectedValues.Contains<string>(item.Key))
            {
                sel = "selected";

            }
            else
                sel = "";
            sb.Append(string.Format("<option "+sel+" value={0}>{1}</option>", item.Key, item.Value));
        }
        return sb.ToString();
    }
}